-- Gamble
-- Created by chengb Feb/3/2016
-- 赌桌格子

require "game/ui/form/dungeon/UIDungeonGamble"

-- 初始化
function initGamble(self)
    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    if not buildingInfo then
        error("获取不到建筑查看配置，元素类型：" .. class);
        return;
    end

    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                return;
            end

            -- 更新建筑访问
            self:updateBuildingVisit();

            -- 不在副本中，不处理
            if not DungeonInstanceM.isInInstance() then
                return;
            end

            local pos = self.gridData:getPos();

            -- 音效
            AudioM.playFx("ui_open1");
            if not DungeonM.isAllMonsterDead() then
                -- 怪物未击杀完毕
                -- 获取赌博记录
                local record = DungeonTentM.getGambleRecord();
                if record == nil then
                    -- 1.未赌过
                    -- 打开赌桌界面
                    UIMgr.getCurrentScene():removeFormByName("UIDungeonGamble");
                    local uiDungeonGamble = UIDungeonGamble.create(pos);
                    UIMgr.getCurrentScene():addForm(uiDungeonGamble);
                elseif record == true then
                    -- 2.赢了
                    self:openBuildingView(nil, 0, getLocStr("gamble_win_dialog"), getLocStr("btn_text_leave"));
                elseif record == false then
                    -- 3.输了
                    self:openBuildingView(nil, 0, getLocStr("gamble_lose_dialog"), getLocStr("btn_text_leave"));
                end
            else
                -- 怪已击杀完毕
                -- 拿走奖励
                local function takeGambleBonus()
                    DungeonActionM.go("take_instance_bonus", pos);
                end

                -- 打开建筑查看界面
                self:openBuildingView(takeGambleBonus);
            end
        end
    end

    -- 建筑图标
    local iconPath = getLevelItemIconPath(buildingInfo["icon"]);

    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.icon:addTouchEventListener(onClicked);
    self.item:addChild(self.icon);
    self.iconPath = iconPath;
end
